var app = getApp();
import commonAPI from '../../api/api'
const {getCurrentPageUrl} = require('../../utils/util');
var ba = wx.getBackgroundAudioManager();
Page({ 

  /**
   * 页面的初始数据
   */
  data: {
    hasPlaying: true,
    userInfo: {},
    wxinfo: {},
    arenaId: '',
    currInfo: {},
    rankingObj: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      arenaId: options.scene
    })
    // 初始化数据
    that.initData();
    setTimeout(() => {
      // 获取用户信息
    that.getwxinfo();
    }, 1000);
  },

  // 用 户信息
  getwxinfo: function () {
    var that = this;
    if(app.globalData.wxUserInfo.avatarUrl == '' || app.globalData.wxUserInfo.avatarUrl == null || app.globalData.wxUserInfo.nickName == '' || app.globalData.wxUserInfo.nickName == null)
     // 跳转登录
     wx.navigateTo({
      url: '../login/login'
    })
  },

  // 初始化数据
  initData: function () {
    var that = this;
    commonAPI.arenaInitDataByArenaId({ arenaId: that.data.arenaId }).then(res => {
      if (res.code == '200') {
        that.setData({
          rankingObj: res.data
        })
        that.getMyarenaInfo();
      }
    })
  },

  // 获取自己的数据 排名，准确度，头像
  getMyarenaInfo() {
    var that = this;
    var temarr = that.data.rankingObj.rankingList;
    var currOpenId = that.data.rankingObj.currInfo == null ? '' : that.data.rankingObj.currInfo.openId;
    temarr.forEach(function (item, i, arr) {
      if (currOpenId == item.openId) {
        item.rankingNum = i + 1;
        that.setData({
          currInfo: item
        })
      }
    })
  },

  // 播放原音
  playorpaush: function () {
    var that = this;
    if (that.data.hasPlaying) {
      ba.src = that.data.rankingObj.myVoiceInfo.voiceSystemURL;
      ba.title = that.data.rankingObj.myVoiceInfo.voiceToen;
      ba.coverImgUrl = that.data.rankingObj.myVoiceInfo.voiceSystemImage;
      ba.onPlay(function () {
        that.setData({
          hasPlaying: false
        })
      })

      // 监听播放是否已经结束
      ba.onEnded(function () {
        that.setData({
          hasPlaying: true
        })
      })

      // 监听背景音频停止事件
      ba.onError(function () {
        that.setData({
          hasPlaying: true
        })
      })

      // 监听背景音频暂停事件
      ba.onPause(function () {
        that.setData({
          hasPlaying: true
        })
      })

      // 监听背景音频停止事件
      ba.onStop(function () {
        that.setData({
          hasPlaying: true
        })
      })
    } else {
      ba.pause();
    }
  },

  // 播放跟读
  playVoice: function (e) {
    let currUserStr = e.currentTarget.dataset.voiceurl
    var that = this, voiceObj = JSON.parse(currUserStr);
    ba.src = voiceObj.tempFilePath;
    ba.title = that.data.rankingObj.myVoiceInfo.voiceToen;
    ba.coverImgUrl = that.data.rankingObj.myVoiceInfo.voiceSystemImage;
    ba.onPlay(function () {
      that.setData({
        hasPlaying: true
      })
    })
  },

  // 和同学一起练
  togetherClassmate: function () {
    var that = this;
    wx.setStorage({
      key: "rankingObj",
      data: that.data.rankingObj
    })
    wx.navigateTo({
      url: '../makerPoster/arenaPoster/arenaPoster',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function (data) {
          console.log(data)
        },
        someEvent: function (data) {
          console.log(data)
        }
      },
      success: function (res) {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { 
          // rankingObj: that.data.rankingObj,qrcodeInfo:r.data
          "voiceResultId": that.data.rankingObj.currInfo.voiceResultId, pageUrl: getCurrentPageUrl()
         })
      }
    })
  },

  // 再比一次
  againBtn: function () {
    var that = this;
    wx.navigateTo({
      url: '../homeworkrepeat/homeworkrepeat',
      events: {
        acceptDataFromOpenedPage: function (data) {
          console.log(data)
        },
        someEvent: function (data) {
          console.log(data)
        }
      },
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', { scene:{ 'arenaId': that.data.arenaId,...that.data.rankingObj.myVoiceInfo,...that.data.currInfo,
        },getIntoType: 'arena', })
      }
    })
  },

  doRepeat: function () {
    this.tooRepeat();
  },

  // 我也试试
  tooRepeat: function () {
    var that = this,
      voiceResultAccuracy = '-';
    if (that.data.currInfo.voiceResultAccuracy) {
      voiceResultAccuracy = +that.data.currInfo.voiceResultAccuracy
    }
    wx.navigateTo({
      url: '../homeworkrepeat/homeworkrepeat',
      events: {
        acceptDataFromOpenedPage: function (data) {
          console.log(data)
        },
        someEvent: function (data) {
          console.log(data)
        }
      },
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', { scene: that.data.rankingObj.myVoiceInfo.voiceSystemId, getIntoType: 'arena', arenaId: that.data.arenaId, voiceResultAccuracy: voiceResultAccuracy })
      }
    })
  },

  open: function () {
    this.setData({
      show: true
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: 'Ta们正在练口语，快来试试吧',
      path: 'pages/arena/arena?scene=' + that.data.arenaId,
    }
  }
})